#include "stat.h"
static inline int chdir(char *path){
	int num = 49;
	int ret;
	asm volatile(
		"lw x17, %1\n"
		"lw x10, %2\n"
		"ecall\n"
		"sd a0, %0"
		:"=m" (ret)
		:"m" (num) , "m" (path)
		:"memory"
	);
	return ret;
}

static inline int do_fstat(int fd, struct stat_fs* st){
	int num = 1073;
	int ret;
	asm volatile(
		"lw x17, %1\n"
		"lw x10, %2\n"
		"lw x11, %3\n"
		"ecall\n"
		"sd a0, %0"
		:"=m" (ret)
		:"m" (num) , "m" (fd), "m"(st)
		:"memory"
	);
	return ret;
}

static inline int do_close(int fd){
	int num=1072;
	int ret;
    asm volatile (
        "lw x17, %1\n"
    	"lw x10,%2\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (num),"m"(fd)
        : "memory"
      );
    return ret;
}
static inline int do_read(int fd,char *buf,size_t n){
	int num = 1070;
	int ret;
	asm volatile(
		"lw x17, %1\n"
		"lw x10, %2\n"
		"lw x11, %3\n"
		"lw x12, %4\n"
		"ecall\n"
		"sd a0, %0"
		:"=m" (ret)
		:"m" (num) , "m" (fd), "m"(buf),"m"(n)
		:"memory"
	);
	return ret;
}
static inline int do_write(int fd,char *buf,size_t n){
	int num = 1071;
	int ret;
	asm volatile(
		"lw x17, %1\n"
		"lw x10, %2\n"
		"lw x11, %3\n"
		"lw x12, %4\n"
		"ecall\n"
		"sd a0, %0"
		:"=m" (ret)
		:"m" (num) , "m" (fd), "m"(buf),"m"(n)
		:"memory"
	);
	return ret;
}
static inline int do_exec(char *path, char **argv){
	int num = 1069;
	int ret;
	asm volatile(
		"lw x17, %1\n"
		"lw x10, %2\n"
		"lw x11, %3\n"
		"ecall\n"
		"sd a0, %0"
		:"=m" (ret)
		:"m" (num) , "m" (path), "m"(argv)
		:"memory"
	);
	return ret;
}
static inline int fork(){
	int num = 170;
	int ret;
	asm volatile(
		"lw x17, %1\n"
		"ecall\n"
		"sd a0, %0"
		:"=m" (ret)
		:"m" (num)
		:"memory"
	);
	return ret;
}
static inline int dup(int fd){
	int num=23;
	int ret;
	asm volatile(
		"lw x17, %1\n"
		"lw x10, %2\n"
		"ecall\n"
		"sd a0, %0"
		:"=m" (ret)
		:"m" (num),"m"(fd)
		:"memory"
	);
	return ret;
}
static inline int wait(int * addr){
	int num=3;
	int ret;
    asm volatile (
        "lw x17, %1\n"
	    "lw x10,%2\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (num),"m"(addr)
        : "memory"
      );
    return ret;
}

static inline int do_open(const char* name, int flags){
	int num=1074;
	int ret;
    asm volatile (
        "lw x17, %1\n"
	     "lw x10, %2\n"
        "lw x11,%3\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (num),"m" (name),"m" (flags)
        : "memory"
      );
    return ret;
}

static inline int do_mknod(const char* name, int major,int minor){
	int num=1027;
	int ret;
    asm volatile (
        "lw x17, %1\n"
	      "lw x10, %2\n"
        "lw x11,%3\n"
        "lw x12,%4\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (num),"m" (name),"m" (major),"m"(minor)
        : "memory"
      );
    return ret;
}
static inline int close(int fd){
	int num=57;
	int ret;
    asm volatile (
        "lw x17, %1\n"
    	"lw x10,%2\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (num),"m"(fd)
        : "memory"
      );
    return ret;
}
static inline int user_syscall(int n, unsigned long a1, unsigned long a2, unsigned long a3) {
	unsigned long long ret;
    asm volatile (
        "lw x17, %1\n"
    	"lw x10,%2\n"
    	"lw x11,%3\n"
    	"lw x12,%4\n"
        "ecall\n"
        "sd a0, %0"
        : "=m" (ret)
        : "m" (n),"m"(a1), "m"(a2), "m"(a3)
        : "memory"
      );
    return ret;
}
static inline int open(const char* name, int flags) {
    return user_syscall(1024, (unsigned long)name, (unsigned long)flags, 0);
}
static inline int read(int fd, char* buf, int len) {
    return user_syscall(63, (unsigned long)fd, (unsigned long)buf, (unsigned long)len);
}
static inline int write(int fd, char* buf, int len) {
    return user_syscall(64, (unsigned long)fd, (unsigned long)buf, (unsigned long)len);
}
/* struct stat {
	dev_t st_dev;
	ino_t st_ino;
	mode_t st_mode;
	nlink_t st_nlink;
	uid_t st_uid;
	gid_t st_gid;
	dev_t st_rdev;
	off_t st_size;
	time_t st_atime;
	time_t st_mtime;
	time_t st_ctime;
	blksize_t st_blksize;
	blkcnt_t st_blocks;
	mode_t st_attr;
};
static inline int fstat(int fd, struct stat* buf) {
    return user_syscall(80, (unsigned long)fd, (unsigned long)buf, 0);
} */
